package controller;


import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import unitils.Chopsticks;
import unitils.Philosopher;

import java.net.URL;
import java.util.ResourceBundle;

public class StartControl implements Initializable {
    @FXML
    private ImageView people3;
    @FXML
    private ImageView people4;
    @FXML
    private ImageView people5;
    @FXML
    private ImageView people1;
    @FXML
    private ImageView chopstick1;
    @FXML
    private ImageView people2;
    @FXML
    private ImageView chopstick3;
    @FXML
    private Button start;
    @FXML
    private ImageView chopstick2;
    @FXML
    private ImageView chopstick5;
    @FXML
    private ImageView chopstick4;
    @FXML
    private TextArea text1;
    @FXML
    private Label text2;
    @FXML
    private Button finish;
    private Philosopher[] philosophers= new Philosopher[5];
    private Chopsticks[] chopsticks = new Chopsticks[5];
    //    打印输出内容
    private TextArea textArea;
    private Service service;
    public StartControl(){}

    @FXML
    void start(ActionEvent event) {
        for (int i = 0; i < 5; i++) {
            philosophers[i].setExit(false);
        }
        this.text1.appendText("Start:\n");

        for (int i = 0; i < 5; i++) {
           Philosopher p = philosophers[i];
            (new Thread(p)).start();
        }
    }

    @FXML
    void exit(ActionEvent event) {
        for (int i = 0; i < 5; i++) {
            philosophers[i].setExit(true);
        }
    }



    @Override
    public void initialize(URL location, ResourceBundle resources) {
//        初始化数据
        service= new Service();
        textArea=this.text1;

        String s=null;
        switch (Service.method){
            case 0:s="说明：本屏幕演示不进行任何处理的哲学家进餐问题，即有可能发生死锁（每个科学家都占用一个筷子并等待其他科学家释放筷子），" +
                    "这种死锁的发生概率在正常情况下微乎其微，本人将思考和等待时间设为1.5秒，吃饭时间设为2秒，死锁的现象比较明显！";break;
            case 1:s="说明：本屏幕演示第一种防死锁方法，即一次性分配所有筷子的方法，破坏了进程资源占有且等待，因此能防止进程死锁!（思考、等待时间：1.5秒，吃饭时间：2秒）";break;
            case 2:s="说明：本屏幕演示第二种防死锁方法，即最多允许4个人拿的左边筷子方法，因此能保证至少有一人能够进餐！（思考、等待时间：1.5秒，吃饭时间：2秒）";break;
            case 3:s="说明：本屏幕演示第三种防死锁方法，即奇数号的哲学家先拿左边筷子，再拿右边筷子；偶数号的哲学家先拿右边筷子，再拿左边筷子，" +
                    "因此总会有一个哲学家能够获得两个筷子而进餐！（思考、等待时间：1.5秒，吃饭时间：2秒）";break;
        }
        text2.setText(s);
        text2.setAlignment(Pos.TOP_LEFT);
        this.chopsticks[0]=new Chopsticks(1,this.chopstick1);
        this.chopsticks[1]=new Chopsticks(2,this.chopstick2);
        this.chopsticks[2]=new Chopsticks(3,this.chopstick3);
        this.chopsticks[3]=new Chopsticks(4,this.chopstick4);
        this.chopsticks[4]=new Chopsticks(5,this.chopstick5);

        this.philosophers[0]=new Philosopher(this.chopsticks[0],this.chopsticks[4],this.people1,1,Service.method,textArea,service);
        this.philosophers[1]=new Philosopher(this.chopsticks[1],this.chopsticks[0],this.people2,2,Service.method,textArea,service);
        this.philosophers[2]=new Philosopher(this.chopsticks[2],this.chopsticks[1],this.people3,3,Service.method,textArea,service);
        this.philosophers[3]=new Philosopher(this.chopsticks[3],this.chopsticks[2],this.people4,4,Service.method,textArea,service);
        this.philosophers[4]=new Philosopher(this.chopsticks[4],this.chopsticks[3],this.people5,5,Service.method,textArea,service);

    }



}
